import QQMap from '../../utils/qq-map.js'
const app = getApp();
Page({
  data: {
    isSearch: false,
    result: [],
    value: '',
    hotCity: app.globalData.hotCity,
    history: [],
    huanhang:'\n',
  },
  inputHandle(e) {
    console.log('eee1111111111111',e)
    const { value } = e.detail
    if (value) {
      QQMap.getSuggestion({
        keyword: value,
        success: (res) => {
          console.log("inputvalue",res.data)
          this.setData({
            isSearch: true,
            result: res.data,
          })
        }
      });
    }
  },
  onLoad() {
    wx.showLoading({
      title: '加载中',
    })
    wx.hideLoading()
    // setTimeout(function () {
    //   wx.hideLoading()
    // }, 500)
    this.setData({
      history: app.getLocal()
    })
  },
  cancelHandle() {
    wx.navigateTo({
      url:'../index/index'
    })
  },
  selectItem(e) {
    console.log("selectItem",e.currentTarget);
    const { lat, lng } = e.currentTarget.dataset
    console.log('1e',e);
    wx.redirectTo({
      url: `../index/index?latitude=${lat}&longitude=${lng}`
    })
  },
  clearHandle() {
    this.setData({
      value: ''
    })
  },
  clearHistory() {
    wx.removeStorage({ key: 'history'})
    this.setData({
      history: []
    })
  },
  dwHandle() {
    wx.redirectTo({
      url: `../index/index`
    })
  },
  hotHandle(e) {
    const { value } = e.currentTarget.dataset
    console.log('e2222222222222',e);
    this.setLocal(value)
    QQMap.geocoder({
      address: value,
      success: (res) => {
        const { location: { lat, lng } } = res.result
        console.log(2222222222222,res);
        wx.redirectTo({
          url: `../index/index?latitude=${lat}&longitude=${lng}`
        })
      }
    });
  },
  // 设置历史记录
  setLocal(val) {
    console.log('历史记录',val)
    const arr = app.getLocal()
    if (!arr.includes(val)) {
      console.log(arr)
      arr.push(val)
      wx.setStorageSync('history', arr)
    }
  },
 
  delHandle(e) {
    const { value } = e.currentTarget.dataset
    const arr = app.getLocal()
    if (arr.includes(value)) {
      const index = arr.indexOf(value)
      arr.splice(index, 1)
      wx.setStorageSync('history', arr)
      this.setData({
        history: arr
      })
    }
  }
})